package com.yun.datastructure.linear.queue.priorityqueue;

import com.yun.datastructure.linear.queue.Queue;

public class PriorityQueue1<E extends Priority> implements Queue<E> {

    Priority[] array;
    int size;

    public PriorityQueue1(int capacity){
        array=new Priority[capacity];
    }

    @Override
    public boolean offer(E value) {
        if(isFull()){
            return false;
        }
        array[size]=value;
        size++;
        return true;
    }

    //返回优先级最高的索引值
    private int selectMax(){
        int max=0;
        for (int i = 1; i < size; i++) {
            if(array[i].priority()>array[max].priority()){
                max=i;
            }
        }
        return max;
    }

    @Override
    public E poll() {
        if(isEmpty()){
            return null;
        }
        int max=selectMax();
        E e = (E) array[max];
        remove(max);
        return e;
    }

    private void remove(int index){
        if(index<size-1){
            System.arraycopy(array,index+1,
                    array,index,size-1-index);
        }
        array[size]=null;
        size--;
    }

    @Override
    public E peek() {
        if(isEmpty()){
            return null;
        }
        int max=selectMax();
        return (E) array[max];
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public boolean isFull() {
        return size==array.length;
    }
}
